package org.come.login;

import io.netty.util.internal.StringUtil;
import org.come.action.FromServerAction;
import org.come.bean.LoginResult;
import org.come.bean.LoginRoleInfo;
import org.come.entity.ChooseArea;
import org.come.socket.Agreement;
import org.come.socket.AgreementUtil;
import org.come.socket.GameClient;
import org.come.socket.SendMessageUntil;
import org.come.test.Main;
import org.come.until.GsonUtil;

public class LoginControl implements FromServerAction {
    @Override
    public void controlMessFromServer(String mes, String type) {
        // TODO Auto-generated method stub
        if (AgreementUtil.rigistersuccess.equals(type)) {// 注册成功
            RegisterSuccess();
        } else if (AgreementUtil.rigistererror.equals(type)) {// 注册失败
            RegisterError(mes);
        } else if (AgreementUtil.loginsuccess.equals(type)) {// 登录成功
            loginSuccess(mes);
        } else if (AgreementUtil.loginerror.equals(type)) {// 登录失败
            loginError(mes);
        } else if (AgreementUtil.createsuccess.equals(type)) {// 创建角色成功
            createRoleSuccess(mes);
        } else if (AgreementUtil.createerror.equals(type)) {// 创建角色失败
            createRoleError(mes);
        }
    }

    /**
     * 用户注册成功
     */
    public void RegisterSuccess() {
        Main.frame.getLoginJpanel().getLoginView().getRegisterView().setVisible(false);
        Main.frame.getLoginJpanel().getLoginView().setMsg("注册成功");
    }

    /**
     * 用户注册失败
     */
    public void RegisterError(String mes) {
        Main.frame.getLoginJpanel().getLoginView().getRegisterView().getLabMsgTip().setText(mes);
    }

    /**
     * 用户登陆成功
     */
    public void loginSuccess(String info) {
        /** zrikka 2020 0415 */
//        RoleTableList roleTableList = GsonUtil.getGsonUtil().getgson().fromJson(info, RoleTableList.class);
//        GameClient.atid = roleTableList.getAtid();
//        GameClient.userid = roleTableList.getUserid();
//        GameClient.username = roleTableList.getUsername();
//        GameClient.userpasw = roleTableList.getPasw();
//
//        Main.frame.getLoginJpanel().setLogin_uid(GameClient.userid);
//        Main.frame.getLoginJpanel().loginSuccess(roleTableList);
        /** zrikka 2020 0415 */

        String[] mesArr = info.split("\\|");
        String atid = mesArr[1];
        String mes = mesArr[0];
        GameClient.atid = atid;

        // 常用角色-添加区编号
        LoginRoleInfo roleInfo = Main.frame.getRoleInfo();
        if (roleInfo.getRoleId() != null) {
            String sendmes = Agreement.getAgreement().enterGameAgreement(roleInfo.getRoleId().toString());
            SendMessageUntil.toServer(sendmes);
        } else {
            /** HGC-2019-12-31 */
            Main.frame.getRoleInfo().setAtId(GameClient.atid);
            Main.frame.getLoginJpanel().setLogin_uid(mes);
            ChooseArea chooseArea = new ChooseArea();
            chooseArea.setQid(GameClient.tempreId.toString());
            chooseArea.setUid(Main.frame.getLoginJpanel().getLogin_uid());
            Main.frame.getLoginJpanel().setRa_id(GameClient.tempreId.toString());
            String sendmes = Agreement.getAgreement().uidAndQidForRoleAgreement(GsonUtil.getGsonUtil().getgson().toJson(chooseArea));
            SendMessageUntil.toServer(sendmes);
        }
    }

    /**
     * 用户登陆失败
     */
    public void loginError(String mes) {
        if (StringUtil.isNullOrEmpty(mes)) {
            Main.frame.getLoginJpanel().getLoginView().setMsg("登录失败，请检查你的用户名密码是否正确");
        } else {
            Main.frame.getLoginJpanel().getLoginView().setMsg(mes);
        }

    }

    /**
     * 创建角色成功
     */
    public void createRoleSuccess(String mes) {
        Main.frame.getLoginJpanel().getCreateView().setMsg("创建成功，正在进入游戏");
        LoginResult loginResult = GsonUtil.getGsonUtil().getgson().fromJson(mes, LoginResult.class);
        String sendmes = Agreement.getAgreement().enterGameAgreement(loginResult.getRole_id().toString());
        SendMessageUntil.toServer(sendmes);
    }

    /**
     * 创建角色失败
     */
    public void createRoleError(String mes) {
        Main.frame.getLoginJpanel().getCreateView().setMsg(mes);
    }
}
